#define _CRT_SECURE_NO_WARNINGS 1
#include "duilie.h"

void QueueInit(Queue* q)
{
	assert(q);
	q->_head = NULL;
	q->_last = NULL;
}

QNode* NewNode(QDataType data)
{
	QNode* ret = (QNode*)malloc(sizeof(QNode));
	ret->_data = data;
	ret->_next = NULL;
	return ret;
}

void QueuePush(Queue* q, QDataType data)
{
	assert(q);
	QNode* newNode = NewNode(data);
	if (q->_head == NULL) {
		q->_head = newNode;
		q->_last = newNode;
	}
	else {
		q->_last->_next = newNode;
		q->_last = newNode;
	}
}

void QueuePop(Queue* q)
{
	assert(q && q->_head != NULL);
	QNode* rem = q->_head;
	q->_head = q->_head->_next;
	free(rem);
	if (q->_head == NULL) {
		q->_last = NULL;
	}
}

QDataType QueueFront(Queue* q)
{
	assert(q && q->_head);
	return q->_head->_data;
}

QDataType QueueBack(Queue* q)
{
	assert(q && q->_last);
	return q->_last->_data;
}

int QueueSize(Queue* q)
{
	assert(q);
	int ret = 0;
	QNode* cut = q->_head;
	while (cut != NULL) {
		ret++;
		cut = cut->_next;
	}
	return ret;
}

int QueueEmpty(Queue* q)
{
	assert(q);
	return q->_head == NULL;
}

void QueueDestroy(Queue* q)
{
	assert(q);
	if (q->_head == NULL) return;
	QNode* cut = q->_head;
	while (cut != NULL) {
		QNode* rem = cut->_next;
		free(cut);
		cut = rem;
	}
	q->_head = q->_last = NULL;
}